﻿using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

public class HelloWorldModule : IHttpModule
{
    public HelloWorldModule()
    {
    }

    public String ModuleName
    {
        get { return "HelloWorldModule"; }
    }

    // In the Init function, register for HttpApplication 
    // events by adding your handlers.
    public void Init(HttpApplication application)
    {
        application.BeginRequest +=
            (new EventHandler(this.Application_BeginRequest));
        application.EndRequest +=
            (new EventHandler(this.Application_EndRequest));
        //application.
    }

    private void Application_BeginRequest(Object source,
         EventArgs e)
    {
        // Create HttpApplication and HttpContext objects to access
        // request and response properties.
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        string filePath = context.Request.FilePath;
        string fileExtension = VirtualPathUtility.GetExtension(filePath);
        if (fileExtension.Equals(".aspx"))
        {
            //context.Response.Redirect("HTMLPage.htm");
            
            context.Response.Write("<h1><font color=red>" +
                "HelloWorldModule: Beginning of Request" +
                "</font></h1><hr>");
        }
        if (fileExtension.Equals(".jpg"))
        {
            string text = "asdjlfk";

            string h = context.Request["H"];
            string w = context.Request["width"];

            HttpResponse Response = context.Response;

            string imgPath = context.Server.MapPath(context.Request.FilePath);
            System.Drawing.Image image = System.Drawing.Image.FromFile(imgPath);
            Bitmap bitmap = new Bitmap(image, image.Width, image.Height);
            Graphics g = Graphics.FromImage(bitmap);

            float fontSize = 12.0f;    //字体大小  
            float textWidth = text.Length * fontSize; //文本的长度  
            ////下面定义一个矩形区域，以后在这个矩形里画上白底黑字  
            //float rectX = 0;
            //float rectY = 0;
            //float rectWidth = text.Length * (fontSize + 8);
            //float rectHeight = fontSize + 8;
            ////声明矩形域  
            //RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight);

            Font font = new Font("宋体", fontSize);   //定义字体  
            //Brush whiteBrush = new SolidBrush(Color.White);   //白笔刷，画文字用  
            //Brush blackBrush = new SolidBrush(Color.Black);   //黑笔刷，画背景用  

            float x = bitmap.Width - textWidth;
            float y = bitmap.Height - 30;

            PointF point = new PointF(x, y);
            Brush brush = new SolidBrush(Color.Blue);

            //g.FillRectangle(blackBrush, rectX, rectY, rectWidth, rectHeight);

            g.DrawString(text, font, brush, point);
            MemoryStream ms = new MemoryStream();
            //保存为Jpg类型  
            bitmap.Save(ms, ImageFormat.Jpeg);

            //输出处理后的图像，这里为了演示方便，我将图片显示在页面中了  
            Response.Clear();
            Response.ContentType = "image/jpeg";
            Response.BinaryWrite(ms.ToArray());
            
            g.Dispose();
            bitmap.Dispose();
            image.Dispose();

            Response.End();
        }
    }

    private void Application_EndRequest(Object source, EventArgs e)
    {
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        string filePath = context.Request.FilePath;
        string fileExtension = VirtualPathUtility.GetExtension(filePath);
        if (fileExtension.Equals(".aspx"))
        {
            context.Response.Write("<hr><h1><font color=red>" +
                "HelloWorldModule: End of Request</font></h1>");
        }
    }
    public void Dispose()
    {
       
    }
}

public enum var1 { Mike = -1, Nike, Jike }